package com.gangquan360.smartadmin.module.customer.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.customer.dao.CustomerCategoryDao;
import com.gangquan360.smartadmin.module.customer.domain.dto.CustomerCategoryDTO;
import com.gangquan360.smartadmin.module.customer.domain.dto.CustomerCategoryQueryDTO;
import com.gangquan360.smartadmin.module.customer.domain.entity.CustomerCategoryEntity;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 客户分类表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-07 17:16:28
 * @since JDK1.8
 */
@Service
public class CustomerCategoryService {

    @Autowired
    private CustomerCategoryDao customerCategoryDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-07 17:16:28
     */
    public ResponseDTO<PageResultDTO<CustomerCategoryDTO>> queryByPage(CustomerCategoryQueryDTO queryDTO) {
        Page<CustomerCategoryDTO> page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<CustomerCategoryEntity> entities = customerCategoryDao.queryByPage(page, queryDTO);
        List<CustomerCategoryDTO> dtoList = SmartBeanUtil.copyList(entities, CustomerCategoryDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<CustomerCategoryDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }
    
    /**
     * 
     * @return
     */
    public ResponseDTO<List<CustomerCategoryDTO>> queryList() {
    	Wrapper<CustomerCategoryEntity> wrapper = new EntityWrapper<CustomerCategoryEntity>();
		List<CustomerCategoryEntity> selectList = customerCategoryDao.selectList(wrapper);
		List<CustomerCategoryDTO> copyList = SmartBeanUtil.copyList(selectList, CustomerCategoryDTO.class);
    	return ResponseDTO.succData(copyList);
    }

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-07 17:16:28
     */
    public ResponseDTO<String> add(CustomerCategoryDTO addDTO) {
        CustomerCategoryEntity entity = SmartBeanUtil.copy(addDTO, CustomerCategoryEntity.class);
        customerCategoryDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-07 17:16:28
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(CustomerCategoryDTO updateDTO) {
        CustomerCategoryEntity entity = SmartBeanUtil.copy(updateDTO, CustomerCategoryEntity.class);
        customerCategoryDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-07 17:16:28
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        customerCategoryDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-07 17:16:28
    */
    public ResponseDTO<CustomerCategoryDTO> detail(Long id){
        CustomerCategoryEntity entity = customerCategoryDao.selectById(id);
        CustomerCategoryDTO dto =  SmartBeanUtil.copy(entity,CustomerCategoryDTO.class);
        return ResponseDTO.succData(dto);
    }
}
